package com.mdd.common.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Calendar;
import java.util.Objects;

public class DateUtils {


    /**
     * 获取n天后 周几
     *
     * @param daysInFuture
     * @return
     */
    public static String dayOfWeek(Integer daysInFuture) {
        if (daysInFuture == 0) {
            return "今天";
        }
        if (daysInFuture == 1) {
            return "明天";
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_YEAR, daysInFuture);
        // 获取星期几
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);
        String weekName;
        switch (dayOfWeek) {
            case Calendar.SUNDAY:
                weekName = "周日";
                break;
            case Calendar.MONDAY:
                weekName = "周一";
                break;
            case Calendar.TUESDAY:
                weekName = "周二";
                break;
            case Calendar.WEDNESDAY:
                weekName = "周三";
                break;
            case Calendar.THURSDAY:
                weekName = "周四";
                break;
            case Calendar.FRIDAY:
                weekName = "周五";
                break;
            case Calendar.SATURDAY:
                weekName = "周六";
                break;
            default:
                weekName = "";
                break;
        }
        return weekName;
    }

    /**
     * 获取n天后 MM-DD
     *
     * @param daysInFuture
     * @return
     */
    public static String getMonthDay(Integer daysInFuture) {
        // 获取当前日期
        LocalDate today = LocalDate.now();
        // 当前日期之后的天数
        LocalDate futureDate = today.plusDays(daysInFuture);
        if (Objects.nonNull(futureDate)) {
            return futureDate.toString().substring(5, 10);
        }
        return "";
    }

    /**
     * 获取几个小时后的时间
     *
     * @param hourInFuture
     * @return
     */
    public static Long getAddNumTime(Integer hourInFuture) {
        // 获取当前日期
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime futureDateTime = now.plusHours(hourInFuture);
        return getLocalDateTime(futureDateTime);
    }

    public static Long getLocalDateTime(LocalDateTime dateTime) {
        // 转换为ZonedDateTime，默认时区
        ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());

        // 转换为Instant
        java.time.Instant instant = zonedDateTime.toInstant();

        // 获取自1970年1月1日00:00:00 GMT以来的毫秒数
        Long millis = instant.toEpochMilli();

        return millis;
    }

    /**
     * 获取当前日期 时间的毫秒
     *
     * @param time
     * @return
     */
    public static Long getLongNowTime(String time) {
        LocalDate now = LocalDate.now();
        LocalDateTime localDateTime = now.atStartOfDay();
        String dateTime = localDateTime.toString().replace("00:00", time);
        localDateTime = LocalDateTime.parse(dateTime);
        return getLocalDateTime(localDateTime);
    }


    public static void main(String[] args) {
        Long nowTime = getLongNowTime("08:30");
        System.out.println("当前日期时间:" + nowTime);

        // 你想要查询的天数
        int daysInFuture = 0;
        System.out.println(daysInFuture + "天后的日期: " + getMonthDay(daysInFuture));
        System.out.println(daysInFuture + "天后是: " + dayOfWeek(daysInFuture));
        System.out.println("几小时后的时间: " + getAddNumTime(2));
    }

}
